﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ptp3.Controllers
{
    public class TorrentController : Controller
    {
        //
        // GET: /Torrent/
        ptp3Entities ptp3db = new ptp3Entities();

        public ActionResult Index()
        {
            
            var torrentsList = ptp3db.torrents.ToList();
            return View(torrentsList);
        }

        //
        // GET: /Torrent/Details/5

        public ActionResult Details(string id)
        {

            var torrent = ptp3db.torrents.Where(t => t.info_hash == id).FirstOrDefault();
            return View(torrent);
        }

        //
        // GET: /Torrent/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Torrent/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                var info_hash = "";
                var torrentList = ptp3db.torrents.Where(f => f.info_hash == info_hash);
                if (torrentList.Any() == false)
                {
                    torrents torr = new torrents();
                    torr.info_hash = info_hash;
                    ptp3db.AddTotorrents(torr);
                }
                ptp3db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Torrent/Edit/5
 
        public ActionResult Edit(string id)
        {
            return View();
        }

        //
        // POST: /Torrent/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Torrent/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Torrent/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
